package com.lexsoft.kanaiscube;


import com.lexsoft.kanaiscube.R;
import com.lexsoft.kanaiscube.util.Tools;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.Toast;

public class Diablo extends FragmentActivity implements OnClickListener,OnTouchListener{
	private Context context;
	private PopupWindow popupWindow;
	private View		popupWindowView;
	private View 		popupWindowAnchor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_diablo);
		context = this;
		initPopupWindow();
		if(findViewById(R.id.container)!=null){
			KanaisCube kc = new KanaisCube();
			kc.setArguments(getIntent().getExtras());
			getSupportFragmentManager().beginTransaction().add(R.id.container, kc).commit();
		}
	}
	TextView[] menu_items;
	private void initPopupWindow(){
		popupWindowAnchor = findViewById(R.id.container);
		popupWindowView = LayoutInflater.from(context).inflate(R.layout.menu_layout, null);
		menu_items = new TextView[] {
				(TextView)popupWindowView.findViewById(R.id.textViewCalc),
				(TextView)popupWindowView.findViewById(R.id.textViewEmpower),
				(TextView)popupWindowView.findViewById(R.id.textViewSkill),
				(TextView)popupWindowView.findViewById(R.id.textViewData),
				(TextView)popupWindowView.findViewById(R.id.textViewKein)
		};
		for(TextView tv : menu_items){
			tv.setOnClickListener(this);
			tv.setOnTouchListener(this);
		}
		popupWindow = new PopupWindow(popupWindowView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.titleColor));
		popupWindow.update();
		popupWindow.setOnDismissListener(new OnDismissListener(){
			public void onDismiss(){
				for(TextView tv : menu_items){
					tv.setBackgroundResource(R.drawable.titleColor);
				}
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.diablo, menu);
		return true;
	}


	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.menuCubeStart:
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}else{
				popupWindow.showAsDropDown(popupWindowAnchor,popupWindowAnchor.getWidth()-popupWindowView.getWidth(),-popupWindowAnchor.getHeight());
			}
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()){
		case R.id.textViewCalc:
			showFragment(CalculatorList.class.getName());
			break;
		case R.id.textViewEmpower:
			break;
		case R.id.textViewSkill:
			break;
		case R.id.textViewData:
			showFragment(DataList.class.getName());
			break;
		case R.id.textViewKein:
			break;
		default:
			Toast.makeText(context, "null", Toast.LENGTH_SHORT).show();
		}
		findViewById(R.id.container).setBackgroundResource(R.drawable.background);
		popupWindow.dismiss();
	}
	private void showFragment(String name){
		Fragment fragment = Tools.loadFragment(name);
		Bundle args = new Bundle();
		fragment.setArguments(args);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.container, fragment);
		ft.addToBackStack(null);
		ft.commit();
	}
	
	@Override
	public boolean onTouch(View view, MotionEvent me) {
		view.setBackgroundResource(R.drawable.selectedColor);
		return false;
	}
}
